use axum::{extract::{FromRequestParts, Path, Request, State}, middleware::Next, response::{IntoResponse, Response}};
use chat_core::User;
use crate::{AppState, error::AppError};


pub async fn verify_chat(State(state): State<AppState>,req: Request,next: Next) -> Response {
    let (mut parts,body) = req.into_parts();
    let Path(chat_id) = Path::<u64>::from_request_parts(&mut parts, &state).await.unwrap();
    //verify if chat exists
    let user = parts.extensions.get::<User>().unwrap();

    if !state
    .is_chat_members(chat_id,user.id as _)
    .await.unwrap_or_default(){
        let err = AppError::CreateChatError(format!(
            "User {} are a member of chat {chat_id}",
            user.id
        ));
        return err.into_response();
    }
    let req = Request::from_parts(parts,body);
    next.run(req).await

}